﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 确认保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var bookName = txtbookname.Text;
            var price = txtprice.Text;
            var author = txtauthor.Text;

            bool hasBookName = !string.IsNullOrEmpty(bookName);
            bool hasPrice = !string.IsNullOrEmpty(price);
            bool hasAuthor = !string.IsNullOrEmpty(author);

            var conString = "server = .; uid = sa; pwd = seventeen111; database = CwordDB";
            SqlConnection connection = new SqlConnection(conString);
            connection.Open();
            SqlCommand command = new SqlCommand("select*from Books", connection);

            if (hasBookName && hasPrice &&hasAuthor)
            {
                MessageBox.Show("保存成功");
                this.Hide();
                //保存后加入数据库中
                command.CommandText = "insert into Books (book_name,price,author) values (@book_name,@price,@author)";
                command.Parameters.AddWithValue("@book_name", bookName);
                command.Parameters.AddWithValue("@price", price);
                command.Parameters.AddWithValue("@author", author);
                command.ExecuteNonQuery();
            }
            else if (txtbookname.Text.ToString().Trim() == "")
            {
                MessageBox.Show("书名不得为空  保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
         
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
